package smartbooks.ui.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import smartbooks.exceptions.MyException;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.helper.CustomerHelper;
import smartbooks.ui.util.CommonConstants;
import smartbooksdb.exception.CustomerException;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Customer;
import smartbooksdb.service.CustomerService;

/**
 *
 * @author Ankit
 */
public class CustomerManager implements CommonConstants {
    /**
     * This method is used to create customer
     * @param customer
     * @return 'true' if the customer is created or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean createCustomer(Customer customer) throws MyException, MandatoryFieldException {
        try {
            System.out.println(customer.toString());
            CustomerService customerService = new CustomerService();
            
            boolean customerPanelValidated = CustomerHelper.validateCustomer(customer);

            if(!customerService.isCustomerNameAvailable(customer.getCustomerName(), true)) {
                throw new MyException(CUSTOMER_NAME, "Customer name unavailable.");
            }
            
            if(customerPanelValidated) {
                long customerId = customerService.createCustomer(customer);
                
                return true;
            }
        } catch (CustomerException ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to update customer
     * @param customer
     * @param editCustomer
     * @return 'true' if the customer is updated or else 'false'
     * @throws MyException
     * @throws MandatoryFieldException
     */
    public static boolean updateCustomer(Customer customer, Customer editCustomer) throws MyException, MandatoryFieldException {
        try {
            System.out.println(customer.toString());
            CustomerService customerService = new CustomerService();
            
            boolean customerPanelValidated = CustomerHelper.validateCustomer(customer);

            if(!customer.getCustomerName().equals(editCustomer.getCustomerName())) {
                if(!customerService.isCustomerNameAvailable(customer.getCustomerName(), true)) {
                    throw new MyException(CUSTOMER_NAME, "Customer name unavailable.");
                }
            }
            
            if(customerPanelValidated) {
                customerService.updateCustomer(customer);
                
                return true;
            }
        } catch (CustomerException ex) {
            Logger.getLogger(CustomerManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to delete customer by updating its status to inactive
     * @param customer
     * @return 'true' if the customer is deleted or else 'false'
     */
    public static boolean deleteCustomer(Customer customer) {
        try {
            CustomerService customerService = new CustomerService();
            
            customer.setUpdatedBy(SmartBooksUserSettings.USERNAME);
            customer.setStatus(INACTIVE);
            customerService.updateCustomer(customer);
            
            return true;
        } catch (CustomerException | MandatoryFieldException ex) {
            Logger.getLogger(ItemManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return false;
    }
    
    /**
     * This method is used to get all active customers
     * @return list of all active customers
     */
    public static ArrayList<Customer> getAllActiveCustomers() {
        CustomerService customerService = new CustomerService();        
        List<Customer> customers = customerService.getAllActiveCustomers();
        
        return (ArrayList<Customer>) customers;
    }
}